<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\ExcelExpoter;
use App\Models\Class_section_group;
use App\Models\Class_type;
use App\Models\Credits_squad;
use App\Models\Student;
use App\Models\Teacher;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Credits;
use Encore\Admin\Widgets\Table;

class CreditsController extends Controller
{
    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('月度学分');
            $content->description(trans('admin.list'));
            
            $content->body($this->grid()->render());
            //$content->body(view('admin.credits_rules'));
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header(trans('admin.teacher'));
            $content->description(trans('admin.edit'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.teacher'));
            $content->description(trans('admin.create'));

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Credits_squad::class, function (Grid $grid) {
            $grid->column('时间')->display(function(){
                return $this->year.'年'.$this->month.'月';
            });
            $grid->squad()->section('班段')->display(function($section){
                return Class_section_group::find($section)->name;
            });
            $grid->squad()->group('分组')->display(function($group){
                return Class_section_group::find($group)->name;
            });
            $grid->squad()->type('班型')->display(function($type){
                return Class_type::find($type)->name;
            });
            $grid->squad()->no('班号');
            $grid->squad()->class_teacher('班主任')->display(function($class_teacher){
                $temp = DB::table('admin_users')->where('id',$class_teacher)->get(['en_name']);
                foreach($temp as $v){
                    return $v->en_name;
                }
            });
            $grid->column('班级人数')->display(function(){
                return DB::table('students')->where('squad_id',$this->squad_id)->count();
            });
            $grid->column('查看学分表')->display(function(){
                return '<a class="fa fa-eye" href="/admin/credits_view/'.$this->id.'"></a>';
            });

            //禁用导出按钮
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->disableCreateButton();

            $grid->disableActions();
            $grid->filter(function ($filter) {
                $filter->column(1/2, function ($filter) {
                    $filter->disableIdFilter();
                    $filter->equal('year','年份')->default(date('Y'));
                    $filter->where(function($query){
                        $query->whereHas('Squad', function ($query) {
                            $query->where('section','=',$this->input);
                        });
                    },'班段')->select(Class_section_group::where('pid','=','0')->get()->pluck('name', 'id'));
                    $filter->where(function($query){
                        $query->whereHas('Squad', function ($query) {
                            $query->where('type','=',$this->input);
                        });
                    },'班型')->select(Class_type::get()->pluck('name', 'id'));
                });
                $filter->column(1/2, function ($filter) {
                    $filter->disableIdFilter();
                    $filter->equal('month','月份')->default(date('m')-1);
                    $filter->where(function($query){
                        $query->whereHas('Squad', function ($query) {
                            $query->where('group','=',$this->input);
                        });
                    },'分组')->select(Class_section_group::where('pid','>','0')->get()->pluck('name', 'id'));
                    $filter->where(function($query){
                        $query->whereHas('Squad', function ($query) {
                            $query->where('no','=',$this->input);
                        });
                    },'班号');
                });
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Teacher::class, function (Form $form) {
            $form->display('id', 'ID');

            $form->text('ch_name', trans('admin.ch_name'))->rules('required')->addElementLabelClass('must-input');
            $form->text('en_name', trans('admin.en_name'))->rules('required')->addElementLabelClass('must-input');
            $form->select('sex', trans('admin.sex'))->options([1 => trans('admin.men'), 2 => trans('admin.women')]);
            $form->email('email', trans('admin.email'));
            $form->mobile('tel', trans('admin.tel'))->options(['mask' => '999 9999 9999']);
            //去掉添加/编辑表单的重置按钮
            $form->disableReset();
        });
    }

    public function rule_save(Request $request)
    {
        foreach($request->request as $v){
            $data = array();
            if(isset($v['total_score-1'])){
                $data['section'] = 1;
            }else{
                $data['section'] = 2;
            }
            foreach($v as $_k => $_v){
                $data[explode('-',$_k)[0]] = $_v;
            }

            $num = DB::table('credits_rules')->where('section','=',$data['section'])->count();

            if($num > 0){
                DB::table('credits_rules')->where('section','=',$data['section'])->update($data);
            }else{
                DB::table('credits_rules')->insert($data);
            }
        }
        return array('message'=>'操作成功！');
    }

    public function credits_view($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('月度学分');
            $content->description(trans('admin.list'));

            $content->body($this->credits_rule_view($id));
            $content->body($this->view_grid($id)->render());
        });
    }

    public function credits_rule_view($id)
    {
        $squad_id = $year = $month = '';
        $credits_squad = DB::table('credits_squads')->where('id',$id)->get();
        foreach($credits_squad as $v){
            $squad_id = $v->squad_id;
            $year = $v->year;
            $month = $v->month;
        }

        $section = $group = $name = $start='';
        $squads = DB::table('squads')->where('id',$squad_id)->get();
        foreach($squads as $v){
            $section = Class_section_group::find($v->section)->name;
            $group = Class_section_group::find($v->group)->name;
            $name = $v->name;
            $start = $v->start_date;
        }

        $string = array();
        $string[] = $name.'/'.$year.'年'.$month.'月月度学分表';
        $string[] = $section.'/'.$group.'/'.date('Y',strtotime($start)).'年'.date('m',strtotime($start)).'月开班';

        $rule = DB::table('credits_rules')->orderBy('section','asc')->get();
        $rule = json_decode(json_encode($rule),true);
        if(count($rule) == 0){
            $rule = '';
        }else{
            $section1 = $rule[0];
            $section2 = $rule[1];
            $rule = array();

            foreach($section1 as $k => $v){
                $rule['section1'][$k.'-1'] = $v;
            }
            foreach($section2 as $k => $v){
                $rule['section2'][$k.'-2'] = $v;
            }

            $rule = json_encode($rule);
        }
        return view('admin.credits_rules',compact('string','rule'));
    }

    protected function view_grid($id)
    {
        $grid = new Grid(new Credits());
        $grid->model()->where('pid',$id);
/*        $grid->student()->no('学号');
        $grid->student()->ch_name('中文名');
        $grid->student()->en_name('英文名');*/


        $grid->column('学号')->display(function(){
            return Student::find($this->student_id)->no;
        });
        $grid->column('中文名')->display(function(){
            return Student::find($this->student_id)->ch_name;
        });
        $grid->column('英文名')->display(function(){
            return Student::find($this->student_id)->en_name;
        });

        $grid->column('详情')->expand(function(){
            $arr = json_decode($this->content,true);
            if(count($arr['kaoqin']) <= 0){
                return '';
            }
            $temp = array_merge($arr['kaoqin'],$arr['test'],$arr['jiaxiao']);
            foreach($temp as $k => $v){
                $temp[$k] = $v."!";
            }
            return new Table(["项目","状态"],$temp);
        }, '展开');

        $grid->kq_credits('考勤学分')->display(function($kq_credits){
            return round($kq_credits,2);
        });

/*        $grid->column('作业')->expand(function(){
            $arr = json_decode($this->content,true);
            if(count($arr['zuoye']) <= 0){
                return '';
            }
            return new Table(['作业','状态'],$arr['zuoye']);
        }, '展开');*/

        $grid->test_credits('测试学分')->display(function($test_credits){
            return round($test_credits,2);
        });

/*        $grid->column('家校')->expand(function(){
            $arr = json_decode($this->content,true);
            if(count($arr['jiaxiao']) <= 0){
                return '';
            }
            return new Table(['家校','状态'],$arr['jiaxiao']);
        }, '展开');*/

        $grid->jx_credits('家校学分')->display(function($jx_credits){
            return round($jx_credits,2);
        });

        $grid->credits('学分总计')->display(function($credits){
            return round($credits,2);
        });

        //禁用导出按钮
        $grid->exporter(new ExcelExpoter($grid,'班级学分记录'));
        //$grid->disableExport();
        //$grid->disableRowSelector();
        $grid->disableCreateButton();

        $grid->disableActions();
        $grid->filter(function ($filter) {
            $filter->column(1/2, function ($filter) {
                $filter->disableIdFilter();
                $filter->like('ch_name',trans('admin.ch_name'));
            });
            $filter->column(1/2, function ($filter) {
                $filter->disableIdFilter();
                $filter->like('en_name',trans('admin.en_name'));
            });
        });
        return $grid;
    }

    public function credits_rule_view_edit()
    {
        Admin::disablePjax();
        return Admin::content(function (Content $content){
            $content->header('学分表');
            $content->description('配置');

            $content->body($this->credits_rule_view_grid());
            //$content->body($this->view_grid($id)->render());
        });
    }

    public function credits_rule_view_grid()
    {
        $rule = DB::table('credits_rules')->orderBy('section','asc')->get();
        $rule = json_decode(json_encode($rule),true);
        if(count($rule) == 0){
            $rule = '';
        }else{
            $section1 = $rule[0];
            $section2 = $rule[1];
            $rule = array();

            foreach($section1 as $k => $v){
                $rule['section1'][$k.'-1'] = $v;
            }
            foreach($section2 as $k => $v){
                $rule['section2'][$k.'-2'] = $v;
            }

            $rule = json_encode($rule);
        }
        return view('admin.credits_rules_edit',compact('rule'));
    }
}
